<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
	<script type="text/javascript">
	//账单列表数据源
	var p1_store = new Ext.data.JsonStore( {
		root : "data",
		totalProperty : "total",
		fields : [ "period","compose_type","filename", "compose_share","compose_page","paper_no","paper_name"
		,"businpnt_no","businpnt_name","card_id","card_name","start_time","end_time","state"],
		url : "work!composeLogGridList.action"
	});
	//账单状态下拉列表
	var state_store = new Ext.data.JsonStore( {
		fields : ["id","text"],
		data:[{id:"",text:"全部"},{id:"0",text:"生成失败"},{id:"1",text:"生成成功"},{id:"2",text:"生成中"},{id:"3",text:"预分发"},{id:"4",text:"分发失败"},{id:"5",text:"分发成功"}]
	});
	//搜索条件账单类型下拉列表
	var type_store1 = new Ext.data.JsonStore( {
		fields : ["id","text"],
		data:[{id:"",text:"全部"},{id:"1",text:"纸质"},{id:"2",text:"电子"}]
	});
	//卡产品下拉列表
	var card_store = new Ext.data.JsonStore( {
		root: "data",
		fields : ["prodId","prodName"],
		url: "product!ProdName.action",
		listeners : {
				"load": function() {
					var RecordName = Ext.data.Record.create([
						{name: "prodId"},
						{name: "prodName"}
					]);
					var newRecord = new RecordName({
						prodId: "",
						prodName: "全部"
					});
					this.insert(0,newRecord);
				}
			}
	});
	//账单类型
	var card_type=new Ext.data.JsonStore( {
		fields : ["id","text"],
		data:[{id:"1",text:"纸质"},{id:"2",text:"电子"},{id:"3",text:"短信"}]
	});
	
	minute = function(rowIndex){
		var record = p1_store.getAt(rowIndex);
		record.data.state = "2";
		compose_pel.getComponent("rule_grid").getStore().remove(record);
		compose_pel.getComponent("rule_grid").getStore().insert(rowIndex,record);
		Ext.Ajax.request({
			params: {"filename":record.data.filename,"period":record.data.period},
			url: 'work!make_composeXml.action',
			success: function(response){
				if("" != response.responseText){
					var res = eval("("+response.responseText+")");
					if(res=="0"){
						alert("账单生成成功！");
					}else{
						alert("生成账单出错！");
					}
				}
			}
		});
	};
	
	var sel = function(){
		p1_store.baseParams = p1_store.baseParams || {};

		p1_store.baseParams["logdata.compose_type"] = Ext.getCmp("a").getValue();
		p1_store.baseParams["logdata.state"] = Ext.getCmp("b").getValue();
		p1_store.baseParams["logdata.card_id"] = Ext.getCmp("c").getValue();
					
		p1_store.load({params:{start:0,limit:limit}});
	};
	var check_select = new Ext.grid.CheckboxSelectionModel({
		checkOnly: true,
		renderer : function(v, p, record){
	        if(record.data['state']=='1' || record.data['state']=='5'){
		    	return '<div class="x-grid3-row-checker">&#160;</div>';
		    } 
		},
		listeners: {
			'beforerowselect': function(thisObj, rowIndex, keepExisting,record  ){
				if(record.data['state']=='1' || record.data['state']=='5'){
					/*-----------选择电子账单时，勾选全部电子账单(待实现)----------------
					if(record.data['compose_type']=='2') {
						for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
							if(this.grid.store.data['compose_type'] == '2' && i != rowIndex) {
								this.selectRow(i, true);
							}
						}
					}
					*/
		    		return true;
		    	} 
		    	return false;
			}
		}
	});
	var compose_pel = new Ext.Panel( {
		id: "rule_panel",
		border : false, 
		layout: "card",
		activeItem:0,
		items:[{
			id: "rule_grid",
			xtype:"grid",
			store:p1_store,
			autoExpandColumn: "filename",
			autoExpandMin: 100,
			headersDisabled: true,
			sm: check_select,
			columns:[
				new Ext.grid.RowNumberer( {header : "序号",width : 30}), 
				check_select,
			    {dataIndex : "period",header : "期别", menuDisabled: true},
			    {dataIndex : "compose_type",header : "类别", menuDisabled: true,
				   renderer:function(input){
	 				  if(""==input)return input;
	          		  var res2=card_type.getById(input);
					  return res2.data.text;
	           	   }
			    },
			    {dataIndex : "filename",header : "文件名称", id:"filename", menuDisabled: true},
			    {dataIndex : "compose_share",header : "账单份数", menuDisabled: true},
			    {dataIndex : "compose_page",	header : "账单页数", menuDisabled: true},
			    {dataIndex : "paper_name",header : "预印纸名称", menuDisabled: true},
			    {dataIndex : "businpnt_name",header : " 外包商 ", menuDisabled: true},
			    {dataIndex : "card_name",header : " 卡产名称 ", menuDisabled: true},
			    {dataIndex : "start_time",header : "开始时间", menuDisabled: true},
			    {dataIndex : "end_time",header : "结束时间", menuDisabled: true},          
			    {dataIndex :"state",header:"状态", menuDisabled: true,
			       renderer:function(input){
	 				  if(""==input)return input;
	          		  var res2=state_store.getById(input);
					  return res2.data.text;
	           	   }
			    },
			    {
			    	header : "操作", 
			    	xtype : "actioncolumn",
			    	id : "recompose",
			    	menuDisabled: true,
			    	renderer: function(input,meta,record,rowIndex){
			    		if("0"==input){
			    			v = '<a id="edit" href="javascript:void(0);" style="color:blue" onclick="new minute('+rowIndex+');">重新合成</a>';
			    			return v;
			    		}else{
			    			v =  '<span>正常</span>';
			    			return v;
			    		}
			    	}
			    }
			],
			buttonAlign: "center",
			tbar:["&nbsp&nbsp类别",
			  {
				id: "a",
				xtype: "billCombox",
				store: 	type_store1,
				width:120,
				value: "",
				listeners: {
					"select": sel
				}
			},"&nbsp&nbsp账单状态",
			 {
				id: "b",
				xtype: "billCombox",
				store: 	state_store,
				width:120,
				value:"",
				listeners: {
					"select": sel
				}
			},"&nbsp&nbsp卡产品",
			 {
				id: "c",
				xtype: "billCombox",
				store: 	card_store,
				valueField: "prodId",
				displayField: "prodName",
				width:120,
				value:"",
				listeners: {
					"select": sel
				}
			}],
			bbar : new Ext.ux.PagingToolbar( {
				pageSize : limit,
				store:p1_store
			}),
			buttons: [{
				text: "分发",
				handler: function(){
					var records = Ext.getCmp("rule_grid").getSelectionModel().getSelections();
					//如果用户尚未选择需要分发的账单，则提示错误信息
					if(records.join("") == ""){
						Ext.Msg.alert("警告","请先选择需要分发的账单，然后点击分发！");
						return false;
					}
					//确定框提示用户是否分发账单
					Ext.Msg.confirm("警告","确定分发账单?",function(button) {
						if(button == "yes") {
							var ids = [];
							for(var i = 0; i < records.length; i++){
								var member = records[i].get("period")+"|";
								member+=records[i].get("compose_type")+"|";
								member+=records[i].get("filename");
								ids.push(member);
							}
							Ext.Ajax.request({
								params: {"listlog":ids},
								url: "work!send.action",
								success: function(response, opts){
									var d=eval("("+response.responseText+")");
									if(d.code == "1"){
										Ext.getCmp("rule_grid").getBottomToolbar().doRefresh();
										Ext.Msg.alert("成功",d.data);
									} else {
										Ext.Msg.alert("失败",d.data);
									}
								}
							});
						}
					});
				}
			}]
		}]
	});
	p1_store.load({params:{start:0,limit:limit}});
	card_store.load({callback: function(){
		Ext.getCmp("c").setValue("");
	}});

	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(compose_pel);
	CenterPanel.doLayout();
</script>